﻿using System;
using System.Collections;
using System.Reflection;
using DataBroker.Dao;

namespace FirebirdDAO
{
    ///<summary>
    /// DAO Class for the Sales table.
    ///</summary>
    public class Sales : Entity
    {
        private String _PO_NUMBER;
        private Int32 _CUST_NO;
        private Int16 _SALES_REP;
        private String _ORDER_STATUS;
        private DateTime _ORDER_DATE;
        private DateTime _SHIP_DATE;
        private DateTime _DATE_NEEDED;
        private String _PAID;
        private Int32 _QTY_ORDERED;
        private Decimal _TOTAL_VALUE;
        private Single _DISCOUNT;
        private String _ITEM_TYPE;
        private Decimal _AGED;

        ///Default constructor
        public Sales()
        {

        }

        /// <summary>
        /// Constructor for joins. 
        /// Available PARENT tables to join with:
        ///    - Customer
        ///    - Employee
        /// </summary>
        public Sales(bool Flat, params Entity[] entities)
            : this()
        {
            foreach (Entity entity in entities)
            {
                string[] entityParts = entity.GetType().ToString().Split('.');
                string entityName = entityParts[entityParts.Length - 1];
                PropertyInfo pf = GetType().GetProperty((entityName));
                if (pf != null)
                {
                    pf.SetValue(this, new EntityCollection(entity), null);
                }
                entityName = entityName + "_";
                pf = GetType().GetProperty((entityName));
                if (pf != null)
                {
                    pf.SetValue(this, entity, null);
                }
            }
            SetFlat(Flat);
        }

        ///Constructor for Joins with flat hierarchy
        public Sales(params Entity[] entities)
            : this(true, entities)
        {
        }

        ///<summary>
        /// Property for column Po_number.
        ///</summary>
        [IsNotNullable]
        [IsPK]
        [MaxLength(8)]
        public String Po_number
        {
            get
            {
                return _PO_NUMBER;
            }
            set
            {
                _PO_NUMBER = value;
            }
        }
        ///<summary>
        /// Property for column Cust_no.
        ///</summary>
        [IsNotNullable]
        public Int32 Cust_no
        {
            get
            {
                return _CUST_NO;
            }
            set
            {
                _CUST_NO = value;
            }
        }
        ///<summary>
        /// Property for column Sales_rep.
        ///</summary>
        public Int16 Sales_rep
        {
            get
            {
                return _SALES_REP;
            }
            set
            {
                _SALES_REP = value;
            }
        }
        ///<summary>
        /// Property for column Order_status.
        ///</summary>
        [IsNotNullable]
        [MaxLength(7)]
        public String Order_status
        {
            get
            {
                return _ORDER_STATUS;
            }
            set
            {
                _ORDER_STATUS = value;
            }
        }
        ///<summary>
        /// Property for column Order_date.
        ///</summary>
        [IsNotNullable]
        public DateTime Order_date
        {
            get
            {
                return _ORDER_DATE;
            }
            set
            {
                _ORDER_DATE = value;
            }
        }
        ///<summary>
        /// Property for column Ship_date.
        ///</summary>
        public DateTime Ship_date
        {
            get
            {
                return _SHIP_DATE;
            }
            set
            {
                _SHIP_DATE = value;
            }
        }
        ///<summary>
        /// Property for column Date_needed.
        ///</summary>
        public DateTime Date_needed
        {
            get
            {
                return _DATE_NEEDED;
            }
            set
            {
                _DATE_NEEDED = value;
            }
        }
        ///<summary>
        /// Property for column Paid.
        ///</summary>
        [MaxLength(1)]
        public String Paid
        {
            get
            {
                return _PAID;
            }
            set
            {
                _PAID = value;
            }
        }
        ///<summary>
        /// Property for column Qty_ordered.
        ///</summary>
        [IsNotNullable]
        public Int32 Qty_ordered
        {
            get
            {
                return _QTY_ORDERED;
            }
            set
            {
                _QTY_ORDERED = value;
            }
        }
        ///<summary>
        /// Property for column Total_value.
        ///</summary>
        [IsNotNullable]
        public Decimal Total_value
        {
            get
            {
                return _TOTAL_VALUE;
            }
            set
            {
                _TOTAL_VALUE = value;
            }
        }
        ///<summary>
        /// Property for column Discount.
        ///</summary>
        [IsNotNullable]
        public Single Discount
        {
            get
            {
                return _DISCOUNT;
            }
            set
            {
                _DISCOUNT = value;
            }
        }
        ///<summary>
        /// Property for column Item_type.
        ///</summary>
        [MaxLength(12)]
        public String Item_type
        {
            get
            {
                return _ITEM_TYPE;
            }
            set
            {
                _ITEM_TYPE = value;
            }
        }
        ///<summary>
        /// Property for column Aged.
        ///</summary>
        public Decimal Aged
        {
            get
            {
                return _AGED;
            }
            set
            {
                _AGED = value;
            }
        }
        #region Customer
        private EntityCollection _Customer;
        ///<summary>
        /// Related table Customer as a collection for hierarchical retrieval.
        ///</summary>
        [NotInTable]
        [TypeContainedAttribute(typeof(CUSTOMER), "Customer")]
        public EntityCollection Customer
        {
            get
            {
                return _Customer;
            }

            set
            {
                _Customer = value;
            }
        }
        private CUSTOMER _Customer_;
        ///<summary>
        /// Related table Customer as a single object for flat retrieval.
        ///</summary>
        [NotInTable]
        public CUSTOMER Customer_
        {
            get
            {
                return _Customer_;
            }

            set
            {
                _Customer_ = value;
            }
        }
        #endregion
        #region Employee
        private EntityCollection _Employee;
        ///<summary>
        /// Related table Employee as a collection for hierarchical retrieval.
        ///</summary>
        [NotInTable]
        [TypeContainedAttribute(typeof(Employee), "Employee")]
        public EntityCollection Employee
        {
            get
            {
                return _Employee;
            }

            set
            {
                _Employee = value;
            }
        }
        private Employee _Employee_;
        ///<summary>
        /// Related table Employee as a single object for flat retrieval.
        ///</summary>
        [NotInTable]
        public Employee Employee_
        {
            get
            {
                return _Employee_;
            }

            set
            {
                _Employee_ = value;
            }
        }
        #endregion
        /// <summary>
        /// Gets join for current entity and table.
        /// </summary>
        /// <param name="tableName">Table to join with entity.</param>
        /// <returns>Join definition for entity and table.</returns>
        public override DBJoin GetJoin(string tableName)
        {
            if (string.Compare(tableName, "Customer", true) == 0)
            {
                ArrayList left = new ArrayList();
                ArrayList right = new ArrayList();
                left.Add(tableName + ".Cust_no");
                right.Add(GetType().Name + ".Cust_no");
                return new DBJoin(left, right, JoinTypeEnumeration.INNER);
            }
            if (string.Compare(tableName, "Employee", true) == 0)
            {
                ArrayList left = new ArrayList();
                ArrayList right = new ArrayList();
                left.Add(tableName + ".Emp_no");
                right.Add(GetType().Name + ".Sales_rep");
                return new DBJoin(left, right, JoinTypeEnumeration.LEFT_OUTER);
            }
            return null;
        }
    }
}


